<?php
namespace app\api\controller;

use app\common\controller\Common;
use think\Db;

class SensitiveCategory {
	protected static $Request;

	function __construct() {
		self::$Request = request()->param();
	}

	/**
	 * 添加敏感词分类 - Api
	 */
	public function createSensitiveCategory() {
		try {
			$res = self::createSensitiveCategoryLogic(self::$Request['name']);
			if ($res->isSuccess) {
				return resultArray(['data' => $res->info]);
			}else {
				return resultArray(['error' => $res->info]);
			}
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	/**
	 * 添加默认敏感词分类 - Api
	 */
	public function createDefaultSensitiveCategory() {
		try {
			$defaultArr = Model('SensitiveCategory')::DEFAULT_ARR;
			foreach ($defaultArr as $key => $val) {
				self::createSensitiveCategoryLogic($val);
			}
			return resultArray(['data' => lang('add_successful')]);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	/**
	 * 查询敏感词分类 - Api
	 */
	public function selectSensitiveCategory() {
		try {
			$whereCondt = [
				Model('SensitiveCategory')::STATUS => 1
			];
			$res = self::selectSensitiveCategoryLogic($whereCondt);
			return resultArray(['data' => $res->data]);
		} catch (Exception $e) {
			return resultArray(['error' => $e->getMessage()]);
		}
	}

	// 添加敏感词分类 - Logic
	protected static function createSensitiveCategoryLogic($name) {
		// 分类是否存在 | 存在不可添加
		$res = self::validateCategoryIsExist($name);
		if ($res->isSuccess) { return operationRes(false, $res->info); }

		// 创建
		$res = self::createSensitiveCategoryCore($name);
		return $res;
    }

    // 分类是否存在 - Validate
  	protected static function validateCategoryIsExist($name) {
  		$whereCondt = [
  			Model('SensitiveCategory')::NAME => $name
  		];
  		$res = self::selectSensitiveCategoryLogic($whereCondt);
  		if (!empty($res->data->list)) {
  			// 存在
  			return operationRes(true, lang('exist'), $res->data->list[0]);
  		}else {
  			// 不存在
  			return operationRes(false, lang('not_exist'));
  		}
  	}

  	// 查询分类 - Logic
  	protected static function selectSensitiveCategoryLogic($whereCondt) {
  		return self::selectSensitiveCategoryCore($whereCondt);
  	}

  	// 添加敏感词分类 - Core
    private static function createSensitiveCategoryCore($name) {
    	$timestamp = time();
    	$createData = [
			Model('SensitiveCategory')::NAME => $name,
			Model('SensitiveCategory')::STATUS => 1,
			Model('SensitiveCategory')::CREATE_TIME => $timestamp,
			Model('SensitiveCategory')::UPDATE_TIME => $timestamp
		];
		if ($res = Model('SensitiveCategory')::create($createData)) {
			// 成功
			return operationRes(true, lang('add_successful'));
		}else {
			// 失败
			return operationRes(false, lang('add_failed'));
		}
    }

  	// 查询敏感词分类 - Core
  	private static function selectSensitiveCategoryCore($whereCondt) {
  		// 查询
  		$list = Model('SensitiveCategory')::where($whereCondt)->select();

  		// 返回结果
  		$returnData = [
  			'list' => $list
  		];
  		return operationRes(true, lang('query_successful'), $returnData);
  	}
}